package com.itheima.tlias08_14.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias08_14.VO.PageVO;
import com.itheima.tlias08_14.dto.SelectEmp;
import com.itheima.tlias08_14.dto.StatisticClazzDTO;
import com.itheima.tlias08_14.dto.StatisticEmpGenderDTO;
import com.itheima.tlias08_14.dto.StatisticEmpJobDTO;
import com.itheima.tlias08_14.entity.Log;
import com.itheima.tlias08_14.mapper.ClazzMapper;
import com.itheima.tlias08_14.mapper.EmpMapper;
import com.itheima.tlias08_14.mapper.LogMapper;
import com.itheima.tlias08_14.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping
public class StatisticController {
    @Autowired
    EmpMapper mapper;
    @Resource
    ClazzMapper clazzMapper;
    @Resource
    LogMapper logMapper;
    @GetMapping("/report/empGenderData")
    public ResultUtil countGender() {
        return ResultUtil.success(mapper.countGender());
    }

    @GetMapping("/report/empJobData")
    public ResultUtil countJob() {
        List<StatisticEmpGenderDTO> sdto = mapper.countJob();
        StatisticEmpJobDTO sejDTO = new StatisticEmpJobDTO();
        String[] strings = new String[sdto.size()];
        Integer[] ins = new Integer[sdto.size()];
        int i = 0;
        for (StatisticEmpGenderDTO s : sdto) {
            strings[i] = s.getName();
            ins[i] = s.getValue();
            i++;
        }
        sejDTO.setJobList(strings);
        sejDTO.setDataList(ins);
        return ResultUtil.success(sejDTO);
    }

    @GetMapping("/report/studentDegreeData")
    public ResultUtil countStudentDegree() {
        return ResultUtil.success(clazzMapper.countStudentDegree());
    }

    @GetMapping("/report/studentCountData")
    public ResultUtil countStudentCount() {
        List<StatisticEmpGenderDTO> cldto = clazzMapper.countClazz();
        StatisticClazzDTO sejDTO = new StatisticClazzDTO();
        String[] strings = new String[cldto.size()];
        Integer[] ins = new Integer[cldto.size()];
        int i = 0;
        for (StatisticEmpGenderDTO s : cldto) {
            strings[i] = s.getName();
            ins[i] = s.getValue();
            i++;
        }
        sejDTO.setClazzList(strings);
        sejDTO.setDataList(ins);
        return ResultUtil.success(sejDTO);
    }

    @GetMapping("/log/page")
    public ResultUtil selectLog(@RequestParam Integer page, @RequestParam Integer pageSize) {

        List<Log> logs = logMapper.selectAllLog();
        Page<Object> page1 = PageHelper.startPage(page, pageSize);
        PageVO<Log> pageVO = new PageVO<>();
        pageVO.setTotal(page1.getTotal());
        pageVO.setRows(logs);
        return ResultUtil.success(pageVO);
    }


//    @GetMapping("/report/empJobData1")
//    public ResultUtil countJob1() {
//        List<Integer> jobs = mapper.selectEmpByJob();
//        Map<String, List<Integer>> mapGroup = jobs.stream().collect(Collectors.groupingBy(new Function<Integer, String>() {
//            @Override
//            public String apply(Integer t) {
//                return switch (t) {
//                    case 1 -> "班主任";
//                    case 2 -> "讲师";
//                    case 3 -> "学工主管";
//                    case 4 -> "教研主管";
//                    case 5 -> "咨询师";
//                    default -> "其他";
//                };
//            }
//        }));
//
//        Set<String> strings = mapGroup.keySet();
////        List<Integer> dataList = mapGroup.values().stream().map((t) -> t.size());
////        HashMap<String, Object> resuLtMap = new HashMap<>();
////        resultMap.put("jobList", jobList);
////        resultMap.put("dataList", dataList);
////        JSONObject jsonObject = new JSONObjectO);
////        jsonObject.put("jobList", jobList);
////        jsonobject.put("dataList", dataList);
////        return Resultutil.success(jsonobject)
//
//
//    }
}